<?php

use yii\db\Migration;

class m180628_124632_categories extends Migration
{
    public function up()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }

        $this->createTable('{{%categories}}', [
            'id' => $this->bigPrimaryKey()->unsigned()->comment('菜单 ID'),
            'parent_id' => $this->bigInteger(20)->notNull()->unsigned()->defaultValue(0)->comment('父级'),
            'name' => $this->string(30)->notNull()->comment('名称'),
            'child' => $this->smallInteger(1)->unsigned()->notNull()->defaultValue(0)->comment('有子级'),
            'parent_arr' => $this->string()->notNull()->defaultValue(0)->comment('父级链'),
            'child_arr' => $this->text()->comment('子级群'),
            'sort' => $this->bigInteger()->unsigned()->notNull()->defaultValue(0)->comment('排序'),
            'status' => $this->smallInteger(1)->notNull()->defaultValue(0)->comment('状态'),
        ], $tableOptions . " COMMENT='分类'");

        $this->createIndex('Parent Id', '{{%categories}}', 'parent_id');
        $this->createIndex('Status', '{{%categories}}', 'status');

        $this->renameColumn('{{%products}}', 'series_id', 'category_id');
        $this->alterColumn('{{%products}}', 'category_id', $this->bigInteger()->unsigned()->comment('分类 ID'));
    }

    public function down()
    {
        $this->dropTable('{{%categories}}');

        $this->renameColumn('{{%products}}', 'category_id', 'series_id');
        $this->alterColumn('{{%products}}', 'series_id', $this->bigInteger()->unsigned()->comment('系列 ID'));
    }
}
